import { mapGetters, mapMutations } from 'vuex'

const refreshMixin = {
  computed: {
    ...mapGetters([
      'refresh'
    ])
  },
  watch: {
    refresh (val) {
      let pageName = this.getKebabCase(val.pageName)
      // console.log(pageName)
      if (val.reload && pageName === this.$options.name) {
        let refresh = Object.assign({}, val)
        refresh.reload = false
        this.set_refresh(refresh)
        this.refreshInfo()
      }
    }
  },
  methods: {
    // 将驼峰命名的字符串转变成短横线命名的字符串
    getKebabCase (str) {
      return str.replace(/[A-Z]/g, (item) => {
        return '-' + item.toLowerCase()
      })
    },
    ...mapMutations({
      'set_refresh': 'SET_REFRESH'
    })
  }
}

export default refreshMixin
